package com.cake.liulanxiangzhu.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cake.liulanxiangzhu.exception.ServiceException;
import com.cake.liulanxiangzhu.mapper.AdminAlbumMapper;
import com.cake.liulanxiangzhu.pojo.dto.AdminPictureDTO;
import com.cake.liulanxiangzhu.pojo.dto.AdminPictureInsertDTO;
import com.cake.liulanxiangzhu.pojo.entity.AdminAlbum;
import com.cake.liulanxiangzhu.pojo.entity.AdminPicture;
import com.cake.liulanxiangzhu.pojo.vo.AdminPictureVO;
import com.cake.liulanxiangzhu.service.AdminPictureService;
import com.cake.liulanxiangzhu.mapper.AdminPictureMapper;
import com.cake.liulanxiangzhu.utils.DataUseUtil;
import com.cake.liulanxiangzhu.web.JsonPage;
import com.cake.liulanxiangzhu.web.ServiceCode;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.File;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author DingHao
 * @description 针对表【admin_picture(图片)】的数据库操作Service实现
 * @createDate 2023-01-22 21:41:30
 */
@Service
@Transactional
public class AdminPictureServiceImpl extends ServiceImpl<AdminPictureMapper, AdminPicture>
        implements AdminPictureService {
    @Autowired
    private AdminPictureMapper adminPictureMapper;

    @Autowired
    private AdminAlbumMapper adminAlbumMapper;

    @Override
    public void addNew(AdminPictureInsertDTO adminPictureInsertDTO) {
        AdminPicture adminPicture = new AdminPicture();
        BeanUtils.copyProperties(adminPictureInsertDTO,adminPicture);
        adminPicture.setCreatTime(new Date());
        adminPicture.setUpdateTime(new Date());
        adminPictureMapper.insert(adminPicture);
    }

    @Override
    public void deletePicture(Integer id) {
        AdminPicture adminPicture = adminPictureMapper.selectById(id);
        if (adminPicture == null){
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND,"数据不存在");
        }
        int i = adminPictureMapper.deleteById(id);
        String url = adminPicture.getUrl();
        new File("e:/files"+url).delete();
        if (i!=1){
            throw new ServiceException(ServiceCode.ERR_DELETE,"系统繁忙请稍后再试");
        }
    }

    @Override
    public void updatePicture(AdminPictureDTO adminPictureDTO) {
        Integer id = adminPictureDTO.getId();
        AdminPicture picture = adminPictureMapper.selectById(id);
        String url = picture.getUrl();
        String urlDTO = adminPictureDTO.getUrl();
        if (DataUseUtil.isNotBlank(urlDTO)){
            if (!url.equals(urlDTO)){
                new File("e:/files"+url).delete();
            }
        }
        AdminPicture adminPicture = new AdminPicture();
        BeanUtils.copyProperties(adminPictureDTO, adminPicture);
        adminPicture.setUpdateTime(new Date());
        int i = adminPictureMapper.updateById(adminPicture);
        if (i != 1) {
            throw new ServiceException(ServiceCode.ERR_DELETE, "系统繁忙，请稍后再试");
        }
    }

    @Override
    public JsonPage<AdminPictureVO> queryList(Integer page, Integer pageSize) {
        PageHelper.startPage(page,pageSize);
        List<AdminPicture> adminPictures = adminPictureMapper.selectList(null);
        List<AdminPictureVO> vos = new ArrayList<>();
        for (AdminPicture adminPicture : adminPictures) {
            AdminPictureVO vo = new AdminPictureVO();
            Integer albumId = adminPicture.getAlbumId();
            AdminAlbum album = adminAlbumMapper.selectById(albumId);
            String name = album.getName();
            BeanUtils.copyProperties(adminPicture,vo);
            vo.setAlbumNm(name);
            vos.add(vo);
        }
        return JsonPage.restPage(new PageInfo<>(vos));
    }

    @Override
    public List<AdminPictureVO> queryAllPicture() {
        return null;
    }

    @Override
    public AdminPictureVO queryById(Integer id) {
        AdminPicture adminPicture = adminPictureMapper.selectById(id);
        AdminPictureVO vo = new AdminPictureVO();
        BeanUtils.copyProperties(adminPicture,vo);
        return vo;
    }


}




